package com.ranjith.struts.customtag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;


public class HelloTag extends BodyTagSupport{

	
	private static final long serialVersionUID = 1L;
	
	private String name;
	private int reps = 5;

	public int getReps() {
		return reps;
	}

	public void setReps(int reps) {
		this.reps = reps;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	@Override
	public int doStartTag() throws JspException {
		System.out.println("Entering doStartTag()");		
		JspWriter out = pageContext.getOut();
		try {
			if(name==null)
				out.println("Hello World");
				out.println("Hello " + name);
				out.newLine();
		}
		catch (Exception e) {
			System.out.println("Error!!!!");
		}		
		return EVAL_BODY_BUFFERED;
	}
	
	public int doAfterBody() throws JspException {
		
		System.out.println("doAfterBody()");
		System.out.println("Value of Reps" + reps);
		BodyContent bd = getBodyContent();		
		for (int i = reps; i-- > 0 ;) {			
			JspWriter out = bd.getEnclosingWriter();
			try {
				out.println((i+1) + bd.getString());				
			} catch (IOException e) {
				e.printStackTrace();
			}		
		}		
			return SKIP_BODY;
		
		
	}
	@Override
	public int doEndTag() throws JspException {
		System.out.println("doEndTag()");
		return EVAL_PAGE;
	}

}
